package com.gather.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.api.service.GatherService;
import com.common.dto.LoginAccount;
import com.common.dto.Proxy;
import com.common.dto.SnatchItem;
import com.common.dto.SnatchTask;
import com.common.dto.WebConfig;
import com.common.dto.WebNode;
import com.common.dto.WebUrl;
import com.common.system.SystemData;
import com.common.utils.CopyUtil;
import com.gather.service.WorkService;

@Service
public class GatherServiceImpl implements GatherService {
	
	@Autowired
	WorkService workService;

	@Override
	public int snatchTask(String webCode, String itemOnlyCode, int status) {
		if(null == webCode || null == itemOnlyCode){
			return -1;
		}
		SnatchItem snatchItem = SystemData.findSnatchItem(webCode, itemOnlyCode);
		if(null == snatchItem) return -2;
		 SnatchTask snatchTask = new SnatchTask();
		 snatchTask.setOnlyCode(SystemData.webConfig.getOnlyCode());
		 snatchTask.setItemOnlyCode(itemOnlyCode);
		 snatchTask.setWebCode(webCode);
		 snatchTask.setGameCode(snatchItem.getGameCode());
		 snatchTask.setItemCode(snatchItem.getItemCode());
		 snatchTask.setItemType(snatchItem.getItemType());
		 snatchTask.setSleepTiem(snatchItem.getSleepTiem());
		 snatchTask.setRatioType(snatchItem.getRatioType());
		 snatchTask.setTaskCode(webCode+"_"+snatchItem.getGameCode()+"_"+snatchItem.getItemCode()+"_"+snatchItem.getItemType());
		 
		 return workService.snatchTask(snatchTask,snatchItem,status);
	}

	@Override
	public int updataWebConfig(WebConfig temWebConfig) {
		if(null == temWebConfig) return -1;
		try {
			List<WebNode> temWebNodes = temWebConfig.getWebNodes();
			for(WebNode temWebNode : temWebNodes){
				WebNode webNode = SystemData.findWebNode(temWebNode.getWebCode());
				if(null == webNode){
					webNode = temWebNode;
					SystemData.addWebNode(webNode);
				}else{
					CopyUtil.Copy(webNode, temWebNode);
					/*webNode.setLoginAuto(temWebNode.isLoginAuto());
					webNode.setLangx(temWebNode.getLangx());
					webNode.setConnectionTimeOut(temWebNode.getConnectionTimeOut());
					webNode.setReadTimeOut(temWebNode.getReadTimeOut());*/
				}
				
				List<WebUrl> temWebUrls = temWebNode.getWebUrls();
				for(WebUrl temWebUrl : temWebUrls){
					WebUrl webUrl = SystemData.findWebUrl(webNode.getWebCode(), temWebUrl.getUrl());
					if(null == webUrl){
						webUrl = temWebUrl;
						SystemData.addWebUrl(webNode.getWebCode(), webUrl);
					}else{
						CopyUtil.Copy(webUrl, temWebUrl);
						//webUrl.setWeight(temWebUrl.getWeight());
					}
					
				}
				
				
				List<Proxy> temProxys = temWebNode.getProxys();
				for(Proxy temProxy : temProxys){
					Proxy proxy = SystemData.findProxy(webNode.getWebCode(), temProxy.getIp());
					if(null == proxy){
						proxy = temProxy;
						SystemData.addProxy(webNode.getWebCode(), proxy);
					}else{
						CopyUtil.Copy(proxy, proxy);
						//webUrl.setWeight(temWebUrl.getWeight());
					}
					
				}
				
				List<LoginAccount> temLoginAccounts = temWebNode.getLoginAccounts();
				for(LoginAccount temLoginAccount : temLoginAccounts){
					LoginAccount loginAccount = SystemData.findLoginAccount(webNode.getWebCode(), temLoginAccount.getUserName());
					if(null == loginAccount){
						loginAccount = temLoginAccount;
						SystemData.addLoginAccount(webNode.getWebCode(), loginAccount);
						
					}else{
						boolean isinit = (loginAccount.getUserPwd().equals(temLoginAccount.getUserPwd()));
						CopyUtil.Copy(loginAccount, temLoginAccount);
						if(!isinit){
							loginAccount.setUserPwd(temLoginAccount.getUserPwd());
							loginAccount.setLogin(false);
							loginAccount.setTryNum(0);
						}
					}
					
				}
				
				List<SnatchItem> temSnatchItems = temWebNode.getSnatchItems();
				for(SnatchItem temSnatchItem : temSnatchItems){
					SnatchItem snatchItem = SystemData.findSnatchItem(webNode.getWebCode(), temSnatchItem.getItemOnlyCode());
					if(null == snatchItem){
						snatchItem = temSnatchItem;
						SystemData.addSnatchItem(webNode.getWebCode(), snatchItem);
					}else{
						CopyUtil.Copy(snatchItem, temSnatchItem);
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return 0;
	}
	
	
}
